package com.mr.cai.main.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jakewharton.rxbinding.view.RxView;
import com.mr.cai.main.MrCaiApplication;
import com.mr.cai.main.R;
import com.mr.cai.main.activity.base.MrCaiBaseActivity;
import com.mr.cai.main.manager.PreferencesManager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import rx.functions.Action1;

/**
 * Created by Administrator on 2015/7/17.
 */
public class WelcomeActivity extends MrCaiBaseActivity {

    private final static int GO_LOGIN = 0x01;
    private final static int GO_MAIN = 0x02;
    private final static int GO_FEATURE = 0x03;

    private RelativeLayout rootView;
    private ViewPager viewpager;
    private MyViewPagerAdapter adapter;
    private int lastX = 0;

    private List<View> views;

    private Handler mHander = new Handler() {
        @Override
        public void handleMessage (Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case GO_LOGIN:
                    LoginActivity.launchActivity(WelcomeActivity.this);
                    WelcomeActivity.this.finish();
                    break;
                case GO_MAIN:
                    MainActivity.launchActivity(WelcomeActivity.this);
                    WelcomeActivity.this.finish();
                    break;
                case GO_FEATURE:
                    rootView.setVisibility(View.GONE);
                    viewpager.setVisibility(View.VISIBLE);
                    break;
            }
        }
    };

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        launch();
    }

    private void launch () {
        rootView = (RelativeLayout) findViewById(R.id.root_view);
        viewpager = (ViewPager) findViewById(R.id.wel_vp);
        initviews();
        adapter = new MyViewPagerAdapter();
        viewpager.setAdapter(adapter);
        viewpager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch (View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastX = (int) event.getX();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if ((lastX - event.getX()) > 100 && (
                                viewpager.getCurrentItem() == views.size() - 1)) {
                            mHander.sendEmptyMessage(GO_LOGIN);
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    default:
                        break;
                }
                return false;
            }
        });

        if (PreferencesManager.getInstance().getFristApp()) {
            mHander.sendEmptyMessageDelayed(GO_FEATURE, 2000);
            PreferencesManager.getInstance().setFristApp(false);
        } else {
            if (TextUtils.isEmpty(PreferencesManager.getInstance().getUserCustomerId())) {
                mHander.sendEmptyMessageDelayed(GO_LOGIN, 1000);
            } else {
                mHander.sendEmptyMessageDelayed(GO_MAIN, 2000);
            }
        }
    }

    private int[] guideBgArray = new int[]{R.drawable.mr_cai_guide_first, R.drawable
            .mr_cai_guide_second, R.drawable.mr_cai_guide_third, R.drawable.mr_cai_guide_fouth};

    /**
     * viewpager
     */
    private void initviews () {
        LayoutInflater inflater = LayoutInflater.from(this);
        views = new ArrayList<View>();
        for (int i = 0; i < 4; i++) {
            View view = inflater.inflate(R.layout.guide_map_layout, null);
            ImageView iv_guide = (ImageView) view.findViewById(R.id.iv_guide);
            iv_guide.setBackgroundResource(guideBgArray[i]);
            if (i == 3) {
                View guideTxt = view.findViewById(R.id.guide_txt);
                RxView.clicks(guideTxt)
                        .throttleFirst(1000, TimeUnit.MILLISECONDS)
                        .subscribe(new Action1<Void>() {
                            @Override
                            public void call (Void aVoid) {
                                mHander.sendEmptyMessage(GO_LOGIN);
                            }
                        });
                guideTxt.setVisibility(View.VISIBLE);
            }
            views.add(view);
        }
    }

    class MyViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount () {
            return views.size();
        }

        @Override
        public boolean isViewFromObject (View view, Object obj) {
            return view == obj;
        }

        @Override
        public Object instantiateItem (View container, int position) {
            View view = views.get(position);
            ((ViewPager) container).addView(view, 0);
            return views.get(position);
        }

        @Override
        public void destroyItem (View container, int position,
                                 Object object) {
            ((ViewPager) container).removeView(views.get(position));
        }
    }


    @Override
    protected void onDestroy () {
        super.onDestroy();
        viewpager.removeAllViewsInLayout();
        viewpager.setAdapter(null);
        viewpager = null;


    }

    /**
     * 返回键处理操作
     */
    @Override
    public boolean onKeyDown (int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //            if(ActivityManager.hasMainActivity()){
            MrCaiApplication.getInstance().exitProcess();
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
